<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
*/
namespace jy_merch;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('fans_api')->方法名();
	其他地方调用：model('jy_merch/fans_api')->方法名();
*/
class fans_api extends \plugin{
	public function get_access_token($wechat,$code){
		if(!$wechat['appid']){
			return false;
		}
		if(!$wechat['appsecret']){
    			$authorizer_access_token=$this->getAuthorizer_accessToken($wechat,$code);
    			return $authorizer_access_token;
    	}else{
    			$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$wechat['appid'].'&secret='.$wechat['appsecret'].'&code='.$code.'&grant_type=authorization_code';
    	}
		$result=file_get_contents($url);
		if(!$result){
			$result=cfc('http')->ihttp_get($url);
			$result=$result['content'];
		}
		$result=json_decode($result,true);
		if($result['errcode']){
			return false;
			exit('获取openid失败：'.$result['errmsg']);
		}
		return $result;
	}
	public function getAuthorizer_accessToken($wechat=array(),$code=''){
		if($wechat['expire'] > TIMESTAMP){
			return $this->wechat['access_token'];
		}
		//通过第三方开放平台获取令牌
		$res=model('core/open')->get_oauth2_token($wechat['appid'],$code);
		$dd=ijson_encode($res);
		return $res;
	}
	//确认token是否有效
	public function check_token($access_token,$openid){
		$url='https://api.weixin.qq.com/sns/auth?access_token='.$access_token.'&openid='.$openid;
		$result=file_get_contents($url);
		if(!$result){
			$result=cfc('http')->ihttp_get($url);
			$result=$result['content'];
		}
		$result=json_decode($result,true);
		if($result['errcode']){
			return false;
		}
		return true;
	}
	public function refresh_token($wechat,$refresh_token){
		$url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$wechat['appid'].'&refresh_token='.$refresh_token.'&grant_type=efresh_token';
		$result=file_get_contents($url);
		if(!$result){
			$result=cfc('http')->ihttp_get($url);
			$result=$result['content'];
		}
		$result=json_decode($result,true);
		if($result['errcode']){
			return false;
			exit('获取openid失败：'.$result['errmsg']);
		}
		return $result;
	}
	public function get_userinfo($access_token,$openid,$code='',$wechat=array()){
		if($_SESSION['container']=='wxwork'){
			$url='https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$access_token.'&code='.$code;
		}else{
			$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
		}
		$result=file_get_contents($url);
		if(!$result){
			$result=cfc('http')->ihttp_get($url);
			$result=$result['content'];
		}
		$result=preg_replace('/[\x00-\x1F]/','',$result);
		$result=preg_replace_callback('/[\xf0-\xf7].{3}/', function($r) { return '';}, $result);
		$result=@json_decode($result,true);
		if($result['errcode']){
			return false;
			exit('获取粉丝详情失败：'.$result['errmsg']);
		}
		if($_SESSION['container']=='wxwork'){
			$result['openid']=$result['OpenId']?:$result['UserId'];
		}
		return $result;
	}
	public function up_userinfo($op='base',$code='',$merchid=0){
		if(!$merchid){
			return false;
		}
		if($_SESSION['openid'.$merchid]){
			return false;
		}
		$onewechat = pdo_getcolumn('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid),'onewechat');
		$onewechat=json_decode($onewechat,true);
		if(!$onewechat || !$onewechat['isopen'] || !$onewechat['appid']){
			return false;
		}
		if($op=='base'){
			//刷新token
			if(TIMESTAMP>$fans['expires_time'] && $fans['refresh_time']>TIMESTAMP){	
					$result=$this->refresh_token($onewechat,$fans['refresh_token']);
			}elseif($fans['access_token'] && $fans['expires_time']>TIMESTAMP){//2小时内,读取
					$result['access_token']=$fans['access_token'];
			}else{
					$result=$this->get_access_token($onewechat,$code);
					$result['expires_in']=$result['expires_in']?:7200;
			}
			$access_token=$result['access_token']?:$fans['access_token'];
			$refresh_token=$result['refresh_token']?:$fans['refresh_token'];
			$expires_time=$result['expires_in']?$result['expires_in']+TIMESTAMP-200:$fans['expires_time'];
			$result['openid']=$result['openid']?:$_SESSION['openid'];
		}
		//再次判断openid
		if(!$result || !$result['openid']){
			return false;
		}
		$_SESSION['openid'.$merchid]=$result['openid'];
		return true;
	}
	//错误码
	public function error_code($code, $errmsg = '未知错误') {//获取错误详情
		global $_AR;
		$errors = array(
			'0'	=> '成功',
			'65400'	=> 'API不可用，即没有开通/升级到新版客服功能',
			'65401'	=> '无效客服帐号',
			'65402'	=> '帐号尚未绑定微信号，不能投入使用',
			'65403'	=> '客服昵称不合法',
			'65404'	=> '客服帐号不合法',
			'65405'	=> '帐号数目已达到上限，不能继续添加',
			'65406'	=> '已经存在的客服帐号',
			'65407'	=> '邀请对象已经是该公众号客服',
			'65408'	=> '本公众号已经有一个邀请给该微信',
			'65409'	=> '无效的微信号',
			'65410'	=> '邀请对象绑定公众号客服数达到上限（目前每个微信号可以绑定5个公众号客服帐号）',
			'65411'	=> '该帐号已经有一个等待确认的邀请，不能重复邀请',
			'65412'	=> '该帐号已经绑定微信号，不能进行邀请',
			'65413'	=> '不存在对应用户的会话信息',
			'65414'	=> '粉丝正在被其他客服接待',
			'65415'	=> '指定的客服不在线',
			'65416' => '查询参数不合法',
			'65417' => '查询时间段超出限制',
			'40001'	=> 'access_token获取失败',
			'40003'	=> '非法的openid',
			'40005'	=> '不支持的媒体类型',
			'40009'	=> '媒体文件长度不合法',
		);
		$code = strval($code);
		if($errors[$code]) {
			return $errors[$code];
		} else {
			$errmsg = $errmsg.',错误代码fansapi：'.$code;
			return $errmsg;
		}
	}
	
}